#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Cog File Reader"""
__author__ = "Lei Chen <hide1713@gmail.com>"
import logging
from data_model.cog import Cog
from genome_exception import GReadFileException
import os.path

class CogFileReader(object):
    """Read Cog File
    """
    def __init__(self, ):
        """
        """
        self.log = logging.getLogger(self.__class__.__name__)

    def read(self, file_name):
        """Return the Cog object map < cog_name, cog_object >
    
        Arguments:
        - `file_name`: the file path
        """
        cog_map = {}
        line_num = 0
        self.log.info("Start loading cog file:" + file_name)
        f=open(file_name, 'r')
        if f:
            base_name = os.path.basename(file_name)
            for line in f:
                try:
                    line_num += 1
                    data_field, desc= line.split("<")
                    data_field = data_field.split()
                    name = data_field[0]
                    length = int(data_field[1])
                    cat = data_field[2]
                    desc = desc[:-1]
                    cog = Cog(name, length, cat, desc)
                except ValueError:
                    raise GReadFileException(file_name,line_num, "Wrong Format")
                cog_map[name] = cog
        f.close()
        self.log.info("Cog file loaded")
        return cog_map
                
            
        

        
        

